package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.CommissionStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgCommissionDetailMapper;
import com.ygqh.baby.po.YgCommissionDetail;
import com.ygqh.baby.po.YgCommissionDetailExample;
import com.ygqh.baby.po.YgUserBalanceDetail;
import com.ygqh.baby.service.YgCommissionDetailService;
import com.ygqh.baby.service.YgUserBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class YgCommissionDetailServiceImpl implements YgCommissionDetailService {

	@Autowired
	private YgCommissionDetailMapper ygCommissionDetailMapper;
	@Autowired
	private YgUserBalanceService ygUserBalanceService;

	@Override
	public List<YgCommissionDetail> find() {
		YgCommissionDetailExample example = new YgCommissionDetailExample();
		example.createCriteria();
		return ygCommissionDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgCommissionDetailExample example = new YgCommissionDetailExample();
		example.createCriteria();
		return ygCommissionDetailMapper.countByExample(example);
	}

	@Override
	public YgCommissionDetail findById(Long id) {
		return ygCommissionDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgCommissionDetail ygCommissionDetail) {
		return ygCommissionDetailMapper.insertSelective(ygCommissionDetail);
	}

	@Override
	public int update(YgCommissionDetail ygCommissionDetail) {
		return ygCommissionDetailMapper.updateByPrimaryKeySelective(ygCommissionDetail);
	}

	@Override
	public ResultSet<YgCommissionDetail> search(QueryInfo queryInfo, Long commissionId, String q) {
		List<YgCommissionDetail> list = ygCommissionDetailMapper.selectSuper(queryInfo, commissionId, q);
		int count = ygCommissionDetailMapper.countSuper(commissionId, q);
		ResultSet<YgCommissionDetail> resultSet = new ResultSet<YgCommissionDetail>(count, list);
		return resultSet;
	}

	@Override
	public int saveOrUpdate(YgCommissionDetail ygCommissionDetail) {
		int r = 0;
		if (ygCommissionDetail.getId() != null) {
			r = this.update(ygCommissionDetail);
		} else {
			r = this.save(ygCommissionDetail);
		}
		return r;
	}

	@Override
	public Message addCommisssionDetail(Long commissionId, BigDecimal commissionPrice, Long userId) {
		List<YgUserBalanceDetail> details = ygUserBalanceService.findBalanceForCommissionDetail(userId);
		List<YgCommissionDetail> list = new ArrayList<YgCommissionDetail>();
		BigDecimal detailPrice = new BigDecimal(0);
		for (YgUserBalanceDetail detail : details) {
			YgCommissionDetail commissionDetail = new YgCommissionDetail();
			list.add(commissionDetail);
			commissionDetail.setBalanceDetailId(detail.getId());
			commissionDetail.setCommissionId(commissionId);
			commissionDetail.setOrderId(detail.getOrderId());
			commissionDetail.setRate(detail.getCommissionRate());
			commissionDetail.setRebateType(detail.getRebateType());
			commissionDetail.setCreateTime(new Date());
			commissionDetail.setPrice(detail.getBalancePrice().subtract(detail.getCommissionDetailPrice()));
			detailPrice = detailPrice.add(detail.getBalancePrice().subtract(detail.getCommissionDetailPrice()));
			if (detailPrice.compareTo(commissionPrice) >= 0) {
				commissionDetail.setPrice(commissionPrice.subtract(detailPrice.subtract(commissionDetail.getPrice())));
				detailPrice = detailPrice.subtract(detail.getBalancePrice().subtract(detail.getCommissionDetailPrice())).add(commissionDetail.getPrice());
				break;
			}
		}
		// 可提现金额小于申请提现金额
		if (detailPrice.compareTo(commissionPrice) < 0) {
			return Message.error("可提现金额小于申请提现金额", detailPrice);
		}
		int row = ygCommissionDetailMapper.addDetailBatch(list);
		return Message.success(row);
	}

	@Override
	public int updateBalanceDetailByCommission(Long userId, Long commissionId, BigDecimal commissionPrice,
											   CommissionStatus commissionStatus, Long maxId, String platformNo) throws Exception {
		// 是否是提交申请单
		Boolean isApply = false;
		// 新增提现申请的操作
		if (commissionPrice != null) {
			isApply = true;
		}
		ygUserBalanceService.updateBalanceDetailByCommission(userId, commissionId, commissionStatus, isApply, maxId, platformNo);
		List<YgUserBalanceDetail> list = ygUserBalanceService.findBalanceDetailByCommissionId(commissionId, userId, platformNo);
		List<YgCommissionDetail> commissionDetails = new ArrayList<>();
		for (YgUserBalanceDetail balance : list) {
			if (balance.getOrderId() != null && balance.getOrderId().indexOf("SQ") == 0) {
				continue;
			}
			YgCommissionDetail detail = new YgCommissionDetail();
			detail.setBalanceDetailId(balance.getId());
			detail.setCommissionId(balance.getCommissionId());
			detail.setOrderId(balance.getOrderId());
			detail.setPrice(balance.getBalancePrice());
			detail.setRate(balance.getCommissionRate());
			detail.setRebateType(balance.getRebateType());
			detail.setCreateTime(balance.getCreateTime());
			commissionDetails.add(detail);
		}
		 ygCommissionDetailMapper.addDetailBatch(commissionDetails);
		return 1;
	}
}
